﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using System.Drawing.Imaging;
using LPV_POS.mh_pos.mh_danhmuc;
using System.Resources;
using System.Globalization;
using LPV_POS.thuvien_cls;

namespace LPV_POS.mh_pos
{
    public partial class mh_danhmuchanghoa : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        thongbao_lang clsthongbao;
        string str_lang = "";
        private byte chu_nhom = 1; // khi lick vao xem chi tiết nhom hàng chu_nhom = 0: xem tất cả nhóm hang; chu_nhom=1: xem nhóm hàng theo nhóm chủ
        private byte chu_nhom_hang = 1; // khi lick vao xem chi tiết hang hoa chu_nhom_hang= 0: xem tất cả hang hoa; chu_nhom_hang=1: xem hàng hóa theo nhóm chủ; chu_nhom_hang= 2: xem hàng hóa theo nhóm hang

        xulyngaythangnam ngaythang;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        int them_sua = -1;
        int ma_nguyenlieu = 0;
        string ma_hanghoa = "";
        string madvt = "";
        string tendvt = "";
        DataTable tb_congthucdinhluong;
        DataTable tb_nguyenlieu;
        string str_arr_macongthuc = "";
        Boolean xoa = false;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                // nhom chu
                label2.Text = rm.GetString("nhomchu", culture);
                bton_tim.Text = rm.GetString("tim", culture);
                label1.Text = rm.GetString("nhomchu", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_ctnhomhang.Text = rm.GetString("xemchitietnhomhang", culture);
                bton_cthanghoa.Text = rm.GetString("xemchitiethanghoa", culture);
                btnDownload.Text = rm.GetString("downloadhinhanh", culture);
                chk_Display_PicItem.Text = rm.GetString("hienthihinhanh", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                this.grid_dmnhomchuhh.GroupPanelText = rm.GetString("danhsachnhomchuhanghoa", culture);
                this.page_nhomchu.Text = rm.GetString("nhomchu", culture);
                this.page_nhomhang.Text = rm.GetString("nhomhang", culture);
                this.page_hanghoa.Text = rm.GetString("mon", culture);
                this.page_nguyenlieu.Text = rm.GetString("nguyenlieu", culture);

                // nhom hang
                label4.Text = rm.GetString("nhomchu", culture);
                label5.Text = rm.GetString("ten", culture);
                label3.Text = rm.GetString("vat", culture);
                bton_timnhomhang.Text = rm.GetString("tim", culture);
                bton_themnhomhang.Text = rm.GetString("them", culture);
                bton_suanhomhang.Text = rm.GetString("sua", culture);
                bton_xoanhomhang.Text = rm.GetString("xoa", culture);
                bton_luunhomhang.Text = rm.GetString("luu", culture);
                bton_xemcthhtheonhomhang.Text = rm.GetString("xemchitiethanghoa", culture);
                bton_thoatnhomhang.Text = rm.GetString("thoat", culture);
                btnNHomHang_Len.Text = rm.GetString("trangtren", culture);
                btnNHomHang_Xuong.Text = rm.GetString("trangduoi", culture);
                btnNHomHang_LuuViTri.Text = rm.GetString("luuvitri", culture);
                this.grid_nhomhh.GroupPanelText = rm.GetString("danhsachnhomhang", culture);
                this.col_tennhomchu.Caption=rm.GetString("nhomchu", culture);
                this.col_tennhomhh.Caption = rm.GetString("ten", culture);

                // hang hoa
                label9.Text = rm.GetString("nhomhang", culture);
                label10.Text = rm.GetString("mon", culture);
                label8.Text = rm.GetString("dvt", culture);
                label25.Text = rm.GetString("barcode", culture);
                label7.Text = rm.GetString("vat", culture);
                label20.Text = rm.GetString("inbepkhac", culture);
                check_banle.Text = rm.GetString("banle", culture);
                label11.Text = rm.GetString("gia_1", culture);
                label12.Text = rm.GetString("gia_2", culture);
                label13.Text = rm.GetString("gia_3", culture);
                label16.Text = rm.GetString("gia_4", culture);
                label15.Text = rm.GetString("gia_5", culture);
                label14.Text = rm.GetString("gia_6", culture);
                label19.Text = rm.GetString("gia_7", culture);
                label18.Text = rm.GetString("gia_8", culture);
                label17.Text = rm.GetString("gia_9", culture);
                bton_timhanghoa.Text = rm.GetString("tim", culture);
                bton_clr.Text = rm.GetString("clr", culture);
                bton_themhanghoa.Text = rm.GetString("them", culture);
                bton_suahanghoa.Text = rm.GetString("sua", culture);
                bton_xoahanghoa.Text = rm.GetString("xoa", culture);
                bton_luuhanghoa.Text = rm.GetString("luu", culture);
                bton_tinhdinhluong.Text = rm.GetString("dinhluong", culture);
                bton_listneed.Text = rm.GetString("danhsachnguyenlieucan", culture);
                bton_thoathanghoa.Text = rm.GetString("thoat", culture);
                this.col_tenhanghoa.Caption = rm.GetString("mon", culture);
                this.col_DVT.Caption = rm.GetString("dvt", culture);
                this.col_banle.Caption = rm.GetString("banle", culture);
                check_banGiaMo.Text = rm.GetString("bangiamo", culture);

                // nguyen lieu
                groupBox1.Text = rm.GetString("thongtinnguyenlieu", culture);
                label22.Text = rm.GetString("ten", culture);
                label6.Text = rm.GetString("dvt", culture);
                label21.Text = rm.GetString("mon", culture);
                label24.Text = rm.GetString("nguyenlieu", culture);
                bton_nguyenlieu_them.Text = rm.GetString("them", culture);
                bton_nguyenlieu_sua.Text = rm.GetString("sua", culture);
                bton_nguyenlieu_xoa.Text = rm.GetString("xoa", culture);
                bton_nguyenlieu_luu.Text = rm.GetString("luu", culture);
                bton_them_dinhluong.Text = rm.GetString("them", culture);
                bton_dinhluong_xoa.Text = rm.GetString("xoa", culture);
                bton_dinhluong_luu.Text = rm.GetString("luu", culture);
                groupBox2.Text = rm.GetString("congthutinhdinhluong", culture);
                btn_Xoa_BOM_LienQuan.Text = rm.GetString("xoadinhluong", culture);
                groupBox1.Text = rm.GetString("thongtinnguyenlieu", culture);
                this.col_nguyenlieu_nguyenlieu.Caption = rm.GetString("nguyenlieu", culture);
                this.col_nguyenlieu_dvt.Caption = rm.GetString("dvt", culture);
                this.col_tinh_tennguyenlieu.Caption = rm.GetString("nguyenlieu", culture);
                this.col_tinh_soluong.Caption = rm.GetString("soluong", culture);
                this.col_tinh_tendvt.Caption = rm.GetString("dvt", culture);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh sach nhom chu hh
        private DataTable f_selectdsnhomchuhh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom hang
        private DataTable f_selectdsnhomhh()
        {
            btnNHomHang_Len.Enabled = true;
            btnNHomHang_LuuViTri.Enabled = true;
            btnNHomHang_Xuong.Enabled = true;

            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            //dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom hang theo nhom chu
        private DataTable f_selectdsnhomhhtheonhomchu(string manhomchuhanghoa)
        {
            btnNHomHang_Len.Enabled = false;
            btnNHomHang_LuuViTri.Enabled = false;
            btnNHomHang_Xuong.Enabled = false;
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MANHOMCHU" };
            o = new object[] { 1, 3, manhomchuhanghoa };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            //dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach hang hoa theo nhom chu
        private DataTable f_selectdshanghoatheonhomchu(string manhomchuhanghoa)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MANHOMCHU" };
            o = new object[] { 1, 4, manhomchuhanghoa };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach hang hoa theo nhom hang hoa
        private DataTable f_selectdshanghoatheonhom(string manhomhanghoa)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 1, 5, manhomhanghoa };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach nhom chu hang hoa
        private DataTable f_selectdsnhomchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm chủ ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // select danh sach don hang hoa

        private DataTable f_selectdshanghoa()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom hang hoa
        private DataTable f_selectdsnhomhhforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            //DataView dv_temp = tb.DefaultView;
            //dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // select danh sach VAT
        private DataTable f_selectdsVATchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 3 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "GIATRI";  //  "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["GIATRI"] = "-1";
            myNewRow["OBJNAME"] = "--- Chọn thuế VAT---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "GIATRI";        //"STT DESC";
            return dv_temp.ToTable();
        }


        // select danh sach don vi tinh
        private DataTable f_selectdsDVTchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 4 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn ĐVT ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom chu hh co dieu kien
        private DataTable f_selectnhomchuhhdk(string tendonvitinh)
        {

            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 1, 2, tendonvitinh };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomchuhh", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // insert them mot nhom chu hang hoa
        private int f_insertnhomchuhh(string tennhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@OBJCODE" };
            o = new object[] { 2, 1, tennhomchuhh, cboLoai.SelectedIndex };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }
        // insert nhom hang
        private int f_insertnhomhang(string tennhomhang, string manhomchu, string mavat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 2, 1, tennhomhang, manhomchu, mavat };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }
        // insert hang hoa

        private int f_inserthanghoa(string tenhanghoa, string manhomhang, string handung, Boolean banle, string mavat, string madvt, string mansx, object hinh, string ghichu,
                                                    decimal gia1, decimal gia2, decimal gia3, decimal gia4, decimal gia5, decimal gia6, decimal gia7, decimal gia8, decimal gia9, string barcode)
        {
            tv = new thuvien();
            object sDM_MAYIN_BEP_MA = cboMayInBep.SelectedValue;
            if (cboMayInBep.SelectedIndex <= 0)
                sDM_MAYIN_BEP_MA = null;

            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMHH ", "@STR_HANDUNG_1", "@BANLE", "@VAT", "@MADVT", "@MANSX", "@HINH","@GHICHU",
                                    "@GIA1","@GIA2","@GIA3","@GIA4","@GIA5","@GIA6","@GIA7","@GIA8","@GIA9","@DM_MAYIN_BEP_MA", "@BAN_GIA_MO", "@BARCODE"};
            o = new object[] { 2, 1, tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx, hinh, ghichu,
                                         gia1, gia2, gia3, gia4, gia5, gia6,  gia7,  gia8,  gia9, sDM_MAYIN_BEP_MA,check_banGiaMo.Checked, barcode};
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }
        // update mot don vi tinh
        private int f_updatenhomchuhh(string manhomchuhh, string tennhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@OBJCODE" };
            o = new object[] { 3, 1, manhomchuhh, tennhomchuhh, cboLoai.SelectedIndex };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }
        // update mot nhom hang
        private int f_updatenhomhang(string manhomhang, string tennhomhang, string manhomchu, string mavat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 3, 1, manhomhang, tennhomhang, manhomchu, mavat };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }
        // update mot hang hoa
        private int f_updatehanghoa(string mahanghoa, string tenhanghoa, string manhomhang, string handung, Boolean banle, string mavat, string madvt, string mansx, object hinh, string ghichu,
                                                    decimal gia1, decimal gia2, decimal gia3, decimal gia4, decimal gia5, decimal gia6, decimal gia7, decimal gia8, decimal gia9, string barcode)
        {
            tv = new thuvien();
            object sDM_MAYIN_BEP_MA = cboMayInBep.SelectedValue;
            if (cboMayInBep.SelectedIndex <= 0)
                sDM_MAYIN_BEP_MA = null;
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@MANHOMHH ", "@STR_HANDUNG_1", "@BANLE", "@VAT", "@MADVT", "@MANSX","@HINH","@GHICHU",
                                        "@GIA1","@GIA2","@GIA3","@GIA4","@GIA5","@GIA6","@GIA7","@GIA8","@GIA9","@DM_MAYIN_BEP_MA", "@BAN_GIA_MO", "@BARCODE"};
            o = new object[] { 3, 1, mahanghoa, tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx,hinh, ghichu,
                                         gia1, gia2, gia3, gia4, gia5, gia6,  gia7,  gia8,  gia9, sDM_MAYIN_BEP_MA,check_banGiaMo.Checked, barcode};
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }
        // xoa nhom chu hang hoa
        private int f_deletenhomchuhh(string manhomchuhh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, manhomchuhh };
            int ketqua = tv.SP_Update("sp_mh_dmnhomchuhh", s, o);
            return ketqua;
        }
        // xoa nhom hh
        private int f_deletenhomhh(string manhomhang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, manhomhang };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }
        // xoa hang hoa
        private int f_deletehanghoa(string mahanghao)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, mahanghao };
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }
        // Tim nhom hang khi nhan nut tim
        private DataTable f_selectfindnhomhang(string tennhomhh, string tenhomchu, string thuevat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 1, 4, tennhomhh, tenhomchu, thuevat };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            return tb;
        }
        // tim thue VAT
        private DataTable f_findnhomhh()
        {
            DataTable tb;
            string tennhomhh;
            string tenhomchu;
            string thuevat;
            if (com_nhomchu.Text != "--- Chọn nhóm chủ ---")
                tenhomchu = com_nhomchu._GetselectedID();
            else
                tenhomchu = "";

            if (com_thueVAT.Text != "--- Chọn thuế VAT---")
                thuevat = com_thueVAT._GetselectedID();
            else
                thuevat = "";


            if (txt_tennhomhh.Text != "")
                tennhomhh = "'%" + txt_tennhomhh.Text + "%'";
            else
                tennhomhh = "";

            tb = f_selectfindnhomhang(tennhomhh, tenhomchu, thuevat);
            return tb;

        }
        // Tim hang hoa khi nhan nut tim
        private DataTable f_selectfindhanghoa(string tenhanghoa, string manhomhang, string handung, string vat, string madvt, Boolean checkhandung, string barcode)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMHH", "@NGAYHANDUNG", "@VAT", "@MADVT", "@CHECK_HANDUNG", "@BARCODE" };
            o = new object[] { 1, 5, tenhanghoa, manhomhang, handung, vat, madvt, checkhandung, barcode.Trim() };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            return tb;
        }

        // tim hang hoa
        private DataTable f_findhh()
        {
            DataTable tb;

            string tenhanghoa;
            string manhomhang;
            //    DateTime handung;

            string thueVAT;
            string DVT;

            Boolean checkhandung;

            if (com_nhomhang.Text != "--- Chọn nhóm hàng ---")
                manhomhang = com_nhomhang._GetselectedID();
            else
                manhomhang = "";

            if (txt_tenhanghoa.Text != "")
                tenhanghoa = "'%" + txt_tenhanghoa.Text + "%'";
            else
                tenhanghoa = "";

            if (com_DVT.Text != "--- Chọn ĐVT ---")
                DVT = com_DVT._GetselectedID();
            else
                DVT = "";

            if (com_vathanghoa.Text != "--- Chọn thuế VAT ---")
                thueVAT = com_vathanghoa._GetselectedID();
            else
                thueVAT = "";



            if (check_handung.Checked == true)
                checkhandung = true;
            else
                checkhandung = false;
            // handung = Convert.ToDateTime(date_handung.Text);
            string ngayhandung = "";
            ngaythang = new xulyngaythangnam();
            if (date_handung.Text != "")
                ngayhandung = ngaythang.chuyenngay(date_handung.Text);


            tb = f_selectfindhanghoa(tenhanghoa, manhomhang, ngayhandung, thueVAT, DVT, checkhandung, txt_barcode.Text);
            return tb;

        }

        // hien thi ds nhom chu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Properties.Items.Clear();
            cb.Datasource = null;
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }

        private void f_nhapgia(TextEdit txt_gia, int x, int y)
        {

            txt_gia.Text = new cpublic().loadformso_point(x, y);
            txt_gia.Focus();
        }

        private void f_enabled_gia_false()
        {
            txt_gia1.Enabled = false;
            txt_gia2.Enabled = false;
            txt_gia3.Enabled = false;
            txt_gia4.Enabled = false;
            txt_gia5.Enabled = false;
            txt_gia6.Enabled = false;
            txt_gia7.Enabled = false;
            txt_gia8.Enabled = false;
            txt_gia9.Enabled = false;
            bton_gia1.Enabled = false;
            bton_gia2.Enabled = false;
            bton_gia3.Enabled = false;
            bton_gia4.Enabled = false;
            bton_gia5.Enabled = false;
            bton_gia6.Enabled = false;
            bton_gia7.Enabled = false;
            bton_gia8.Enabled = false;
            bton_gia9.Enabled = false;
        }

        private void f_enabled_gia_true()
        {
            txt_gia1.Enabled = true;
            txt_gia2.Enabled = true;
            txt_gia3.Enabled = true;
            txt_gia4.Enabled = true;
            txt_gia5.Enabled = true;
            txt_gia6.Enabled = true;
            txt_gia7.Enabled = true;
            txt_gia8.Enabled = true;
            txt_gia9.Enabled = true;
            bton_gia1.Enabled = true;
            bton_gia2.Enabled = true;
            bton_gia3.Enabled = true;
            bton_gia4.Enabled = true;
            bton_gia5.Enabled = true;
            bton_gia6.Enabled = true;
            bton_gia7.Enabled = true;
            bton_gia8.Enabled = true;
            bton_gia9.Enabled = true;
        }

        // Begin dinh luong
        private DataTable f_select_dinhluong(int flag, int choose, string mahh)
        {
            tv = new thuvien();
            DataTable tb = tv.SP_SelectDataTable("sp_dinhluong", 
                                                new string[] { "@flag", "@choose", "@mahh" }, 
                                                new object[] { flag, choose, mahh });
            return tb;

        }

        private int f_update_dinhluong(int flag, int choose, int manguyenlieu, string tennguyenlieu, string dvt, string mahh)
        {
            int ketqua = -1;
            tv = new thuvien();
            ketqua = tv.SP_Update("sp_dinhluong", new string[] { "@flag", "@choose", "@manguyenlieu", "@tennguyenlieu", "@dvt", "@mahh" },
                                                                          new object[] { flag, choose, manguyenlieu, tennguyenlieu, dvt, mahh });
            return ketqua;

        }

        private DataTable f_bangdinhluong()
        {
            DataTable tb_dinhluong = new DataTable();
            tb_dinhluong.Columns.Add("MAHANGHOA", typeof(string));
            tb_dinhluong.Columns.Add("MANGUYENLIEU", typeof(int));
            tb_dinhluong.Columns.Add("MADVT", typeof(string));
            tb_dinhluong.Columns.Add("TENHANGHOA", typeof(string));
            tb_dinhluong.Columns.Add("TENNGUYENLIEU", typeof(string));
            tb_dinhluong.Columns.Add("SOLUONG", typeof(string));
            tb_dinhluong.Columns.Add("MADVT", typeof(string));
            return tb_dinhluong;
        }
        // End dinh luong

        public mh_danhmuchanghoa()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            f_showdataoncombox(com_nhomchu, f_selectdsnhomchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_thueVAT, f_selectdsVATchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_nhomhang, f_selectdsnhomhhforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_vathanghoa, f_selectdsVATchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_DVT, f_selectdsDVTchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_nguyenlieu_dvt, f_selectdsDVTchuforcom(), "OBJID", "OBJNAME");

        }

        private void mh_danhmuchanghoa_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            string sSQL = "SELECT DM_MAYIN_BEP_MA = NULL, OBJNAME=N' Chọn' " +
                          "UNION " +
                          "SELECT DM_MAYIN_BEP_MA, OBJNAME FROM DM_MAYIN_BEP";
            DataTable tbMayInBep = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            cboMayInBep.DataSource = tbMayInBep;

            luoi_nhomchuhh.MainView = grid_dmnhomchuhh;
            luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();

            tb_nguyenlieu = f_select_dinhluong(1, 1, "");
            grd_nguyenlieu.DataSource = tb_nguyenlieu;

            // tb_congthucdinhluong = f_select_dinhluong(1,2,"");
            // tb_congthucdinhluong.Columns.Add(new DataColumn("FLAG", typeof(Boolean)));

            //col_delete.FieldName = "FLAG";
            //col_delete.Caption = "";
            // luoi_congthucdinhluong.DataSource = tb_congthucdinhluong;


            bton_tennhomchuhh.Enabled = false;

            bton_luunhomhang.Enabled = false;
            grid_hanghoa.OptionsBehavior.Editable = false;
            col_banle.FieldName = "BANLE";

            check_handung.Checked = false;
            bton_luuhanghoa.Enabled = false;
            bton_gia1.Enabled = false;
            bton_gia2.Enabled = false;
            bton_gia3.Enabled = false;
            bton_gia4.Enabled = false;
            bton_gia5.Enabled = false;
            bton_gia6.Enabled = false;
            bton_gia7.Enabled = false;
            bton_gia8.Enabled = false;
            bton_gia9.Enabled = false;
            chk_Display_PicItem.Checked = ocpublic.Registry_Get("chk_Display_PicItem") == "True";

            decimal inum_Item_SizeX = 0;
            decimal.TryParse(ocpublic.Registry_Get("num_Item_SizeX"), out inum_Item_SizeX);
            if (inum_Item_SizeX > 0)
                num_Item_SizeX.Value = inum_Item_SizeX;

            decimal inum_Item_SizeY = 0;
            decimal.TryParse(ocpublic.Registry_Get("num_Item_SizeY"), out inum_Item_SizeY);
            if (inum_Item_SizeY > 0)
                num_Item_SizeY.Value = inum_Item_SizeY;


            toolTip1.SetToolTip(num_Item_SizeX, "93");
            toolTip1.SetToolTip(lbl_Item_SizeX, "93");
            toolTip1.SetToolTip(num_Item_SizeY, "75");
            toolTip1.SetToolTip(lbl_Item_SizeY, "75");
        }

        private void grid_dmnhomchuhh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_dmnhomchuhh.FocusedColumn;
            txt_tennhomchuhh.Text = grid_dmnhomchuhh.GetRowCellDisplayText(chiso, col_tennhomchuhh) + "";

            int icode = 0;
            int.TryParse(grid_dmnhomchuhh.GetRowCellDisplayText(chiso, col_OBJCODE) + "", out icode);
            cboLoai.SelectedIndex = icode;

            bton_luu.Enabled = false;
            bton_tennhomchuhh.Enabled = false;
        }

        private void grid_nhomhh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_nhomhh.FocusedColumn;
            txt_tennhomhh.Text = grid_nhomhh.GetRowCellDisplayText(chiso, col_tennhomhh) + "";
            string tenthuevat = grid_nhomhh.GetRowCellDisplayText(chiso, col_vat) + "";
            tenthuevat = tenthuevat == "" ? "0%" : tenthuevat;
            com_thueVAT.Text = tenthuevat;
            string tennhomchu = grid_nhomhh.GetRowCellDisplayText(chiso, col_tennhomchu) + "";
            com_nhomchu.Text = tennhomchu;
            bton_tennhomhh.Enabled = false;
            bton_luunhomhang.Enabled = false;

            string manhomhanghoa = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh) + "";
            if (manhomhanghoa != "")
                luoi_hanghoa.DataSource = f_selectdshanghoatheonhom(manhomhanghoa);
        }

        private void grid_hanghoa_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = grid_hanghoa;
            int chiso = grid_hanghoa.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_hanghoa.FocusedColumn;

            com_nhomhang.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tennhomhang) + "";
            txt_tenhanghoa.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenhanghoa) + "";
            com_DVT.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_DVT) + "";
            com_vathanghoa.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenthueVAT) + "";
            date_handung.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_handung) + "";
            txt_gia1.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia1) + "";
            txt_gia2.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia2) + "";
            txt_gia3.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia3) + "";
            txt_gia4.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia4) + "";
            txt_gia5.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia5) + "";
            txt_gia6.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia6) + "";
            txt_gia7.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia7) + "";
            txt_gia8.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia8) + "";
            txt_gia9.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_gia9) + "";

            string banGiaMo = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_BAN_GIA_MO) + "";
            if (banGiaMo == "True")
                check_banGiaMo.Checked = true;
            else
                check_banGiaMo.Checked = false;

            object mayinbep = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_DM_MAYIN_BEP_MA);
            if (mayinbep + "" == "")
                mayinbep = System.DBNull.Value;
            try
            {
                cboMayInBep.SelectedValue = mayinbep;
            }
            catch {
                mayinbep = System.DBNull.Value;
                cboMayInBep.SelectedValue = mayinbep;
            }
            if (cboMayInBep.Text == "")
                cboMayInBep.SelectedIndex = 0;

            string sOBJID = grid_hanghoa.GetRowCellDisplayText(chiso, col_mahanghoa) + "";
            object oHinh = null;
            DataTable tbHH = null;
            if (luoi_hanghoa.DataSource != null)
            {
                tbHH = (DataTable)luoi_hanghoa.DataSource;

                if (tbHH.Select("OBJID='" + sOBJID + "'").Length > 0)
                {
                    oHinh = tbHH.Select("OBJID='" + sOBJID + "'")[0]["hinh"];
                }
            }

            panel_hinh.BackgroundImage = new cpublic().ConvertByteArrayToImage(oHinh);

            string banle = grid_hanghoa.GetRowCellDisplayText(chiso, col_banle) + "";
            if (banle == "Checked")
                check_banle.Checked = true;
            else if (banle == "Unchecked")
                check_banle.Checked = false;

            bton_tenhanghoa.Enabled = false;

            bton_luuhanghoa.Enabled = false;
            bton_gia1.Enabled = false;
            bton_gia2.Enabled = false;
            bton_gia3.Enabled = false;
            bton_gia4.Enabled = false;
            bton_gia5.Enabled = false;
            bton_gia6.Enabled = false;
            bton_gia7.Enabled = false;
            bton_gia8.Enabled = false;
            bton_gia9.Enabled = false;
            check_banle.Visible = true;
            //lbel_banle.Visible = true;
            txt_barcode.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_barcode) + "";
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            string tennhomchuhh = "";
            if (txt_timtennhomchuhh.Text != "")
                tennhomchuhh = "'%" + txt_timtennhomchuhh.Text + "%'";
            else
                tennhomchuhh = txt_timtennhomchuhh.Text;
            luoi_nhomchuhh.DataSource = f_selectnhomchuhhdk(tennhomchuhh);
        }

        private void bton_timnhomhang_Click(object sender, EventArgs e)
        {
            if (bton_tim.Text == "CLR")
            {
                com_nhomchu.Text = "--- Chọn nhóm chủ --";
                com_thueVAT.Text = "--- Chọn thuế VAT---";
                txt_tennhomhh.Text = "";
                bton_tim.Text = "Tìm";
            }
            else
            {
                luoi_nhomhh.DataSource = null;
                luoi_nhomhh.DataSource = f_findnhomhh();
                bton_tim.Text = "CLR";
            }
        }

        private void bton_timhanghoa_Click(object sender, EventArgs e)
        {

            luoi_hanghoa.DataSource = null;
            luoi_hanghoa.DataSource = f_findhh();

            check_handung.Visible = false;
            f_enabled_gia_true();
            this.grid_hanghoa_FocusedRowChanged(null, null);
        }

        private void bton_tennhomchuhh_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;

            //Variable.str_position_frmchu = "bton_tendonvitinh";
            //frm_chu.ShowDialog();
            //txt_tennhomchuhh.Text = Variable.str_chu;
            txt_tennhomchuhh.Text = new cpublic().loadformchu_point(300, 250);
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tennhomchuhh.Text = "";
            bton_luu.Enabled = true;
            bton_tennhomchuhh.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_themnhomhang_Click(object sender, EventArgs e)
        {
            txt_tennhomhh.Text = "";
            com_nhomchu._SetselectedID("1");
            com_thueVAT._SetselectedID("1");
            bton_tennhomhh.Enabled = true;
            bton_luunhomhang.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_themhanghoa_Click(object sender, EventArgs e)
        {
            txt_tenhanghoa.Text = "";
            com_nhomhang._SetselectedID("1");
            com_vathanghoa._SetselectedID("1");
            com_DVT._SetselectedID("1");
            //date_handung.Text = DateTime.Today.ToString("dd/MM/yyyy");
            date_handung.DateTime = DateTime.Today;
            txt_gia1.Text = "";
            txt_gia2.Text = "";
            txt_gia3.Text = "";
            txt_gia4.Text = "";
            txt_gia5.Text = "";
            txt_gia6.Text = "";
            txt_gia7.Text = "";
            txt_gia8.Text = "";
            txt_gia9.Text = "";
            bton_gia1.Enabled = true;
            bton_gia2.Enabled = true;
            bton_gia3.Enabled = true;
            bton_gia4.Enabled = true;
            bton_gia5.Enabled = true;
            bton_gia6.Enabled = true;
            bton_gia7.Enabled = true;
            bton_gia8.Enabled = true;
            bton_gia9.Enabled = true;
            bton_tenhanghoa.Enabled = true;
            bton_luuhanghoa.Enabled = true;
            Variable.str_trangthaithemsua = "them";
            check_banle.Visible = true;
            //lbel_banle.Visible = true;
            check_handung.Checked = false;
            txt_barcode.Text = "";
            bton_barcode.Enabled = true;
            panel_hinh.BackgroundImage = new cpublic().ConvertByteArrayToImage(null);
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_tennhomchuhh.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_suanhomhang_Click(object sender, EventArgs e)
        {
            bton_tennhomhh.Enabled = true;
            bton_luunhomhang.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_suahanghoa_Click(object sender, EventArgs e)
        {
            bton_tenhanghoa.Enabled = true;
            bton_luuhanghoa.Enabled = true;
            bton_gia1.Enabled = true;
            bton_gia2.Enabled = true;
            bton_gia3.Enabled = true;
            bton_gia4.Enabled = true;
            bton_gia5.Enabled = true;
            bton_gia6.Enabled = true;
            bton_gia7.Enabled = true;
            bton_gia8.Enabled = true;
            bton_gia9.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
            check_banle.Visible = true;
            //lbel_banle.Visible = true;
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            int ketqua;
            if (luoi_nhomchuhh.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manhomchuhh = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh) + "";
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            clsthongbao.show_info(str_lang,"chatchan");
            if (Variable.thongbao == true)
            {
                ketqua = f_deletenhomchuhh(manhomchuhh);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_nhomchuhh.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_nhomchuhh.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_xoanhomhang_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            int ketqua;
            if (luoi_nhomhh.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manhomhang = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh) + "";
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            clsthongbao.show_info(str_lang, "chatchan");
            if (Variable.thongbao == true)
            {
                ketqua = f_deletenhomhh(manhomhang);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_nhomhh.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_nhomhh.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_xoahanghoa_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            int ketqua;
            if (luoi_hanghoa.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string mahanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa) + "";
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            clsthongbao.show_info(str_lang, "chatchan");
            if (Variable.thongbao == true)
            {
                ketqua = f_deletehanghoa(mahanghoa);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongchatchan");
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_hanghoa.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_hanghoa.Views[0].DataSource;
                tam.Delete(chiso);
                check_banle.Visible = true;
                //lbel_banle.Visible = true;
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            int ketqua;

            if (txt_tennhomchuhh.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            int chiso = grid_dmnhomchuhh.FocusedRowHandle;

            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertnhomchuhh(txt_tennhomchuhh.Text);

                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang,"khongthanhcong");
                    return;
                }
                this.f_showdataoncombox(com_nhomchu, f_selectdsnhomchuforcom(), "OBJID", "OBJNAME");
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string manhomchuhh = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh) + "";
                ketqua = f_updatenhomchuhh(manhomchuhh, txt_tennhomchuhh.Text);

                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
            }
            luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            if (Variable.str_trangthaithemsua == "sua")
                grid_dmnhomchuhh.FocusedRowHandle = chiso;
        }

        private void bton_luunhomhang_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            ngaythang = new xulyngaythangnam();
            if (txt_tennhomhh.Text == "" || com_nhomchu.Text == "--- Chọn nhóm chủ ---")
                return;
            string tennhomhh = txt_tennhomhh.Text;
            string tennhomchu = com_nhomchu._GetselectedID();
            string thueVAT = "";
            if (com_thueVAT.Text == "--- Chọn thuế VAT---")
                thueVAT = "";
            else
                thueVAT = com_thueVAT._GetselectedID();
            int chiso = grid_nhomhh.FocusedRowHandle;
            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertnhomhang(tennhomhh, tennhomchu, thueVAT);

                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                this.f_showdataoncombox(com_nhomhang, f_selectdsnhomhhforcom(), "OBJID", "OBJNAME");
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string manhomhang = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh) + "";
                ketqua = f_updatenhomhang(manhomhang, tennhomhh, tennhomchu, thueVAT);

                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }
                // luoi_donvitinh.DataSource = f_selectdonvitinh();
            }
            luoi_nhomhh.DataSource = null;
            luoi_nhomhh.DataSource = f_selectdsnhomhh();
            if (Variable.str_trangthaithemsua == "sua")
                grid_nhomhh.FocusedRowHandle = chiso;
            else if (Variable.str_trangthaithemsua == "sua")
                grid_nhomhh.FocusedRowHandle = grid_nhomhh.RowCount - 1;
        }

        private void bton_luuhanghoa_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            ngaythang = new xulyngaythangnam();
            if (txt_tenhanghoa.Text == "" || com_nhomhang.Text == "--- Chọn nhóm hàng ---")
                return;

            int chiso = -1;

            object oHinh = null;
            if (panel_hinh.BackgroundImage != null)
                oHinh = new cpublic().ConvertImageToByteArray(ImageFormat.Gif, panel_hinh.BackgroundImage);
            string tenhanghoa = txt_tenhanghoa.Text;
            string manhomhang = com_nhomhang._GetselectedID();
            string handung;
            if (check_handung.Checked == true)
                handung = ngaythang.chuyenngay(date_handung.Text);
            else
                handung = "";
            Boolean banle;
            if (check_banle.Checked == true)
                banle = true;
            else
                banle = false;

            string mavat = com_thueVAT._GetselectedID();
            string madvt = com_DVT._GetselectedID();
            string mansx = "";
            string ghichu = "";
            string thueVAT;
            if (com_vathanghoa.Text == "--- Chọn thuế VAT---")
                thueVAT = "";
            else
                thueVAT = com_vathanghoa._GetselectedID();

            decimal gia1 = Convert.ToDecimal("0" + txt_gia1.Text.Replace(",", ""));
            decimal gia2 = Convert.ToDecimal("0" + txt_gia2.Text.Replace(",", ""));
            decimal gia3 = Convert.ToDecimal("0" + txt_gia3.Text.Replace(",", ""));
            decimal gia4 = Convert.ToDecimal("0" + txt_gia4.Text.Replace(",", ""));
            decimal gia5 = Convert.ToDecimal("0" + txt_gia5.Text.Replace(",", ""));
            decimal gia6 = Convert.ToDecimal("0" + txt_gia6.Text.Replace(",", ""));
            decimal gia7 = Convert.ToDecimal("0" + txt_gia7.Text.Replace(",", ""));
            decimal gia8 = Convert.ToDecimal("0" + txt_gia8.Text.Replace(",", ""));
            decimal gia9 = Convert.ToDecimal("0" + txt_gia9.Text.Replace(",", ""));

            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                #region "   them"
                string sName = this.KiemTra_TrungBarcode("").Trim();
                if (sName!="")
                {
                    MessageBox_LPV.Show("Trùng barcode với " + sName.ToUpper());
                    return;
                }
                ketqua = f_inserthanghoa(tenhanghoa, manhomhang, handung, banle, thueVAT, madvt, mansx, oHinh, ghichu,
                                                         gia1, gia2, gia3, gia4, gia5, gia6, gia7, gia8, gia9, txt_barcode.Text);

                if (ketqua == -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }                                
                #endregion
                clsthongbao.show_info(str_lang, "thanhcong");
                //MessageBox_LPV.Show("Thêm thành công");
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                #region "   sua"
                chiso = ((ColumnView)luoi_hanghoa.Views[0]).FocusedRowHandle;
                string mahanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa) + "";
                //
                string sName = this.KiemTra_TrungBarcode(mahanghoa).Trim();
                if (sName != "")
                {
                    MessageBox_LPV.Show("Trùng barcode với " + sName.ToUpper());
                    return;
                }                
                ketqua = f_updatehanghoa(mahanghoa, tenhanghoa, manhomhang, handung, banle, thueVAT, madvt, mansx, oHinh, ghichu,
                                                             gia1, gia2, gia3, gia4, gia5, gia6, gia7, gia8, gia9, txt_barcode.Text);
                if (ketqua == -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                #endregion
                //MessageBox_LPV.Show("Sửa thành công");
                clsthongbao.show_info(str_lang, "thanhcong");
            }

            luoi_hanghoa.DataSource = null;
            luoi_hanghoa.DataSource = this.f_selectdshanghoatheonhom(manhomhang);//30110401
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grid_hanghoa.SelectRow(chiso);
                grid_hanghoa.FocusedRowHandle = chiso;
            }
        }

        #region "   KiemTra_TrungBarcode"
        string KiemTra_TrungBarcode(string sMaHangHoa)
        {
            if (txt_barcode.Text.Trim() != "")
            {
                
                thuvien othuvien = new thuvien();
                string sSQL = "SELECT OBJNAME FROM DM_HH WHERE BARCODE = '" + txt_barcode.Text.Trim() + "' ";
                if (sMaHangHoa != "")
                    sSQL += "AND OBJID <> '" + sMaHangHoa + "'";
                DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                if (tb.Rows.Count > 0)
                {
                    return tb.Rows[0]["OBJNAME"] + "";
                }
            }
            return "";
        }        
        #endregion
        
        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

            this.Close();
            //}
        }

        private void bton_thoatnhomhang_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

            this.Close();
            //}
        }

        private void bton_tennhomhh_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;

            //Variable.str_position_frmchu = "mhdmnhomhhc_bton_tennhomhh";
            //frm_chu.ShowDialog();
            //txt_tennhomhh.Text = Variable.str_chu;
            txt_tennhomhh.Text = new cpublic().loadformchu_point(250, 200);
        }

        private void bton_thoathanghoa_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

            this.Close();
            //}
        }

        private void bton_ctnhomhang_Click(object sender, EventArgs e)
        {
            // hien thi danh sach nhom hang hoa theo nhom chu
            chu_nhom = 0;
            //string manhomchuhanghoa = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh).ToString();
            //luoi_nhomhh.MainView = grid_nhomhh;
            //luoi_nhomhh.DataSource = null;
            //luoi_nhomhh.DataSource = f_selectdsnhomhhtheonhomchu(manhomchuhanghoa);
            tab_hanghoa.SelectedTabPage = page_nhomhang;
        }

        private void bton_cthanghoa_Click(object sender, EventArgs e)
        {
            // hien thi danh sach hang hoa 
            chu_nhom_hang = 0;
            //string manhomchuhanghoa = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh).ToString();
            //luoi_hanghoa.MainView = grid_hanghoa;
            //luoi_hanghoa.DataSource = null;
            //luoi_hanghoa.DataSource = f_selectdshanghoatheonhomchu(manhomchuhanghoa);
            tab_hanghoa.SelectedTabPage = page_hanghoa;
        }

        private void bton_xemcthhtheonhomhang_Click(object sender, EventArgs e)
        {
            // hien thi danh sach hang hoa theo nhom hang
            chu_nhom_hang = 2;
            tab_hanghoa.SelectedTabPage = page_hanghoa;
        }

        private void tab_hanghoa_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (tab_hanghoa.SelectedTabPage.Name == "page_nhomhang")
            {
                #region "   page_nhomhang"
                if (chu_nhom == 0)  // khi click vao bton: xem chi tiet nhom hang
                {
                    string manhomchuhanghoa = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh) + "";
                    luoi_nhomhh.MainView = grid_nhomhh;
                    luoi_nhomhh.DataSource = null;
                    luoi_nhomhh.DataSource = f_selectdsnhomhhtheonhomchu(manhomchuhanghoa);
                }
                else
                {
                    luoi_nhomhh.MainView = grid_nhomhh;
                    luoi_nhomhh.DataSource = null;
                    luoi_nhomhh.DataSource = f_selectdsnhomhh();
                }
                if (grid_nhomhh.RowCount > 0)
                {
                    txt_tennhomhh.Text = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_tennhomhh) + "";
                    string tenthuevat = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_vat) + "";
                    com_thueVAT.Text = tenthuevat;
                    string tennhomchu = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_tennhomchu) + "";
                    com_nhomchu.Text = tennhomchu;

                }

                chu_nhom = 1;
                com_nhomchu.Datasource = null;

                f_showdataoncombox(com_nhomchu, f_selectdsnhomchuforcom(), "OBJID", "OBJNAME");
                com_nhomhang.Datasource = null;
                f_showdataoncombox(com_nhomhang, f_selectdsnhomhhforcom(), "OBJID", "OBJNAME");
                #endregion
            }
            else if (tab_hanghoa.SelectedTabPage.Name == "page_hanghoa") // khi click vao bton xem hang hoa chi tiet
            {
                #region "   page_hanghoa"
                switch (chu_nhom_hang)
                {
                    case 0:
                        string manhomchuhanghoa = grid_dmnhomchuhh.GetRowCellValue(grid_dmnhomchuhh.FocusedRowHandle, col_manhomchuhh) + "";
                        luoi_hanghoa.MainView = grid_hanghoa;
                        luoi_hanghoa.DataSource = null;
                        luoi_hanghoa.DataSource = f_selectdshanghoatheonhomchu(manhomchuhanghoa);
                        break;
                    case 1:
                        //luoi_hanghoa.MainView = grid_hanghoa;
                        //luoi_hanghoa.DataSource = null;
                        //luoi_hanghoa.DataSource = f_selectdshanghoa();

                        string manhomhanghoa = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh) + "";
                        //if (manhomhanghoa != "")
                        //    luoi_hanghoa.DataSource = f_selectdshanghoatheonhom(manhomhanghoa);
                        //else
                        //    luoi_hanghoa.DataSource = f_selectdshanghoa();
                        break;
                    case 2:
                        luoi_hanghoa.MainView = grid_hanghoa;
                        luoi_hanghoa.DataSource = null;
                        manhomhanghoa = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh) + "";
                        luoi_hanghoa.DataSource = f_selectdshanghoatheonhom(manhomhanghoa);
                        break;
                }

                com_nhomhang.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tennhomhang) + "";
                txt_tenhanghoa.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tenhanghoa) + "";
                com_DVT.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_DVT) + "";
                com_vathanghoa.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tenthueVAT) + "";
                date_handung.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_handung) + "";
                txt_gia1.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia1) + "";
                txt_gia2.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia2) + "";
                txt_gia3.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia3) + "";
                txt_gia4.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia4) + "";
                txt_gia5.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia5) + "";
                txt_gia6.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia6) + "";
                txt_gia7.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia7) + "";
                txt_gia8.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia8) + "";
                txt_gia9.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia9) + "";

                string banGiaMo = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_BAN_GIA_MO) + "";
                if (banGiaMo == "")
                    check_banGiaMo.Checked = true;
                else
                    check_banGiaMo.Checked = false;

                string mayinbep = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_DM_MAYIN_BEP_MA) + "";
                cboMayInBep.Text = mayinbep;
                if (cboMayInBep.Text == "")
                    cboMayInBep.SelectedIndex = 0;

                txt_gia9.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_gia9) + "";

                string sOBJID = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa) + "";
                object oHinh = null;
                DataTable tbHH = null;
                if (luoi_hanghoa.DataSource != null)
                {
                    tbHH = (DataTable)luoi_hanghoa.DataSource;

                    if (tbHH.Select("OBJID='" + sOBJID + "'").Length > 0)
                    {
                        oHinh = tbHH.Select("OBJID='" + sOBJID + "'")[0]["hinh"];
                    }
                }

                panel_hinh.BackgroundImage = new cpublic().ConvertByteArrayToImage(oHinh);

                string banle = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_banle) + "";
                if (banle == "Checked")
                    check_banle.Checked = true;
                else if (banle == "Unchecked")
                    check_banle.Checked = false;

                chu_nhom_hang = 1;
                #endregion
            }


            //   f_showdataoncombox(com_nhomchu, f_selectdsnhomchuforcom(), "OBJID", "OBJNAME");


        }

        private void bton_browser_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF";

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                txt_pathhinh.Text = dlg.FileName;
                //pichinh_anh.ImageLocation = dlg.FileName;
                Image img = Image.FromFile(dlg.FileName);
                panel_hinh.BackgroundImage = img;
            }
        }
        private void bton_gia1_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia1, 400, 350);
        }

        private void bton_gia2_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia2, 400, 350);
        }

        private void bton_gia3_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia3, 400, 350);
        }

        private void bton_gia4_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia4, 400, 350);
        }

        private void bton_gia5_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia5, 400, 350);
        }

        private void bton_gia6_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia6, 400, 350);
        }

        private void bton_gia7_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia7, 400, 350);
        }

        private void bton_gia8_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia8, 400, 350);
        }

        private void bton_gia9_Click(object sender, EventArgs e)
        {
            f_nhapgia(txt_gia9, 400, 350);
        }

        private void bton_tenhanghoa_Click(object sender, EventArgs e)
        {
            txt_tenhanghoa.Text = new cpublic().loadformchu_point(300, 200);
        }

        private void bton_nhapkhohang_Click(object sender, EventArgs e)
        {
            txt_timtennhomchuhh.Text = new cpublic().loadformchu_point(300, 250);
        }

        private void check_handung_CheckedChanged(object sender, EventArgs e)
        {
            if (check_handung.Checked == true)
                //date_handung.Text =DateTime.Now.ToShortDateString();
                date_handung.DateTime = DateTime.Today;

            else
                date_handung.Text = "";
        }

        private void date_handung_TextChanged(object sender, EventArgs e)
        {
            if (date_handung.Text == "")
                check_handung.Checked = false;
            else
                check_handung.Checked = true;
        }

        private void page_nhomchu_Paint(object sender, PaintEventArgs e)
        {

        }

        #region "   Thay Doi ViTri"
        private void btnNHomHang_Len_Click(object sender, EventArgs e)
        {
            try
            {
                int ihandle = grid_nhomhh.FocusedRowHandle;
                DataTable tb = (DataTable)luoi_nhomhh.DataSource;
                if (ihandle > 0)
                {
                    this.swapDataRow(tb, ihandle - 1, ihandle);
                    grid_nhomhh.FocusedRowHandle = ihandle - 1;
                    grid_nhomhh.SelectRow(ihandle - 1);
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void btnNHomHang_Xuong_Click(object sender, EventArgs e)
        {
            try
            {
                int ihandle = grid_nhomhh.FocusedRowHandle;
                DataTable tb = (DataTable)luoi_nhomhh.DataSource;
                if (ihandle < tb.Rows.Count - 1)
                {
                    this.swapDataRow(tb, ihandle, ihandle + 1);
                    grid_nhomhh.FocusedRowHandle = ihandle + 1;
                    grid_nhomhh.SelectRow(ihandle + 1);
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        DataTable swapDataRow(DataTable _tb, int _i1, int _i2)
        {
            try
            {
                DataRow drTMP = _tb.Copy().NewRow();
                drTMP.ItemArray = _tb.Rows[_i1].ItemArray;
                _tb.Rows[_i1].ItemArray = _tb.Rows[_i2].ItemArray;
                _tb.Rows[_i2].ItemArray = drTMP.ItemArray;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return _tb;
        }

        private void bton_luunhomhang_ViTri_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tb = (DataTable)luoi_nhomhh.DataSource;
                string sSQL = "";
                thuvien othuvien = new thuvien();
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    sSQL = "UPDATE DM_NHOMHH SET SAPXEP = " + (i + 1) + " WHERE OBJID LIKE '" + tb.Rows[i]["OBJID"] + "' ";
                    othuvien.SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                }
                if (tb.Rows.Count > 0)
                    MessageBox_LPV.Show("Lưu vị trí thành công.");
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        private void bton_clr_Click(object sender, EventArgs e)
        {
            com_nhomhang.Text = "--- Chọn nhóm hàng ---";
            com_vathanghoa.Text = "--- Chọn thuế VAT ---";
            com_DVT.Text = "--- Chọn ĐVT ---";
            txt_tenhanghoa.Text = "";

            date_handung.DateTime = DateTime.Today;

            check_banle.Visible = false;

            check_handung.Visible = true;
            check_handung.Checked = false;
            f_enabled_gia_true();
            bton_tenhanghoa.Enabled = true;
        }

        #region "Dinh luong"
        private void bton_nguyenlieu_them_Click(object sender, EventArgs e)
        {
            them_sua = 0;    // trang thai them moi nguyen lieu
            txt_nguyenlieu_ten.Text = "";
            com_nguyenlieu_dvt._SetselectedID("1");
        }

        private void bton_nguyenlieu_sua_Click(object sender, EventArgs e)
        {
            if (grv_nguyenlieu.RowCount > 0)
                them_sua = 1;    // trang thai sua moi nguyen lieu
            else
                them_sua = -1;

        }

        private void bton_nguyenlieu_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (grd_nguyenlieu.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manguyenlieu = grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_ma) + "";
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_update_dinhluong(4, 1, int.Parse(manguyenlieu), "", "", "");
                if (ketqua <= -1)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)grd_nguyenlieu.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)grd_nguyenlieu.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_nguyenlieu_luu_Click(object sender, EventArgs e)
        {
            if (txt_nguyenlieu_ten.Text == "")
            {
                return;
            }
            if (com_nguyenlieu_dvt._GetselectedID() == "1")
            {
                return;
            }
            int ketqua = -1;

            if (them_sua == 0)  // truong hop them
            {

                ketqua = f_update_dinhluong(2, 1, 0, txt_nguyenlieu_ten.Text, com_nguyenlieu_dvt._GetselectedID(), "");
                if (ketqua > -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm thành công", loaithongbao.canhbao.GetHashCode());
                    DataRow dr = tb_nguyenlieu.NewRow();
                    dr["TENNGUYENLIEU"] = txt_nguyenlieu_ten.Text;
                    dr["MADVT"] = com_nguyenlieu_dvt._GetselectedID();
                    dr["TENDVT"] = com_nguyenlieu_dvt.Text;
                    tb_nguyenlieu.Rows.InsertAt(dr, 0);
                    tb_nguyenlieu = f_select_dinhluong(1, 1, "");//2011.11.23
                    grd_nguyenlieu.DataSource = tb_nguyenlieu;//
                    grv_nguyenlieu.FocusedRowHandle = 0;
                    them_sua = -1;
                    return;
                }
                else
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());

                    return;
                }
            }
            else if (them_sua == 1)           // truong hop sua
            {

                int manguyenlieu = int.Parse(grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_ma) + "");
                ketqua = f_update_dinhluong(3, 1, manguyenlieu, txt_nguyenlieu_ten.Text, com_nguyenlieu_dvt._GetselectedID(), "");
                if (ketqua > -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Sửa thành công", loaithongbao.canhbao.GetHashCode());
                    grv_nguyenlieu.SetFocusedRowCellValue(col_nguyenlieu_nguyenlieu, txt_nguyenlieu_ten.Text);
                    grv_nguyenlieu.SetFocusedRowCellValue(col_nguyenlieu_dvt, com_nguyenlieu_dvt.Text);
                    tb_nguyenlieu = f_select_dinhluong(1, 1, "");//2011.11.23
                    grd_nguyenlieu.DataSource = tb_nguyenlieu;//
                    int chiso = ((ColumnView)grd_nguyenlieu.Views[0]).FocusedRowHandle;
                    grv_nguyenlieu.FocusedRowHandle = chiso;

                    #region "thay doi cong thuc nguyen lieu"
                    //        tb_congthucdinhluong.Rows.Clear();
                    grd_congthucdinhluong.DataSource = tb_congthucdinhluong;
                    tb_congthucdinhluong = f_select_dinhluong(1, 2, txt_mahanghoa.Text.Trim());
                    tb_congthucdinhluong.Columns.Add(new DataColumn("FLAG", typeof(Boolean)));
                    col_delete.FieldName = "FLAG";
                    col_delete.Caption = "";
                    grd_congthucdinhluong.DataSource = tb_congthucdinhluong;
                    #endregion

                    return;
                }
                else
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
        }
        private void grid_nguyenlieu_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grv_nguyenlieu.RowCount > 0)
            {
                txt_nguyenlieu_ten.Text = grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_nguyenlieu) + "";
                com_nguyenlieu_dvt._SetselectedID(grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_madvt) + "");
            }
            btn_Xoa_BOM_LienQuan.Enabled = grv_nguyenlieu.FocusedRowHandle >= 0;
        }
        private void bton_nguyenlieu_ten_Click(object sender, EventArgs e)
        {
            string nguyenlieu_ten = new cpublic().loadformchu_point(300, 250);
            if (nguyenlieu_ten != "")
                txt_nguyenlieu_ten.Text = nguyenlieu_ten;
        }


        private void bton_quatrai_Click(object sender, EventArgs e)
        {
            if (grv_nguyenlieu.RowCount > 0)
            {
                int chiso = grv_nguyenlieu.FocusedRowHandle;
                txt_dinhluong_nguyenlieu.Text = grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_nguyenlieu) + "";
                if (grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_ma) + "" == "")
                {
                    tb_nguyenlieu = f_select_dinhluong(1, 1, "");
                    grd_nguyenlieu.DataSource = tb_nguyenlieu;
                    grv_nguyenlieu.FocusedRowHandle = chiso;
                }
                ma_nguyenlieu = int.Parse(grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_ma) + "");
                madvt = grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_madvt) + "";
                tendvt = grv_nguyenlieu.GetRowCellValue(grv_nguyenlieu.FocusedRowHandle, col_nguyenlieu_dvt) + "";
                this.bton_them_dinhluong_Click(null, null);
            }
        }

        private void bton_tinhdinhluong_Click(object sender, EventArgs e)
        {
            if (grid_hanghoa.RowCount > 0)
            {
                txt_dinhluong_mathang.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tenhanghoa) + "";
                ma_hanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa) + "";
                txt_mahanghoa.Text = ma_hanghoa;
                if (tb_congthucdinhluong != null)
                    tb_congthucdinhluong.Rows.Clear();
                grd_congthucdinhluong.DataSource = tb_congthucdinhluong;
                tb_congthucdinhluong = f_select_dinhluong(1, 2, ma_hanghoa);
                tb_congthucdinhluong.Columns.Add(new DataColumn("FLAG", typeof(Boolean)));
                col_delete.FieldName = "FLAG";
                col_delete.Caption = "";
                grd_congthucdinhluong.DataSource = tb_congthucdinhluong;
                tab_hanghoa.SelectedTabPage = page_nguyenlieu;
            }

        }
           
        private void bton_them_dinhluong_Click(object sender, EventArgs e)
        {
            if (tb_congthucdinhluong != null)
            {
                if (tb_congthucdinhluong.Select("MANGUYENLIEU LIKE '" + ma_nguyenlieu + "'").Length == 0)
                {
                    DataRow dr = tb_congthucdinhluong.NewRow();
                    dr["MAHH"] = ma_hanghoa;
                    dr["MANGUYENLIEU"] = ma_nguyenlieu;
                    dr["MADVT"] = madvt;
                    dr["TENNGUYENLIEU"] = txt_dinhluong_nguyenlieu.Text;
                    dr["SOLUONG"] = "1";
                    dr["TENHH"] = txt_dinhluong_mathang.Text;
                    dr["TENDVT"] = tendvt;
                    dr["FLAG"] = false;
                    tb_congthucdinhluong.Rows.InsertAt(dr, 0);
                    grv_congthucdinhluong.FocusedRowHandle = 0;
                }
            }
        }
        private void bton_dinhluong_xoa_Click(object sender, EventArgs e)
        {
            if (tb_congthucdinhluong == null)
                return;
            if (tb_congthucdinhluong.Rows.Count < 0)
                return;

            

            // lay id của công thuc tinh dinh luong
            // str_arr_macongthuc = "";
            DataTable tb_temp = tb_congthucdinhluong.GetChanges();
            if (tb_temp == null)
            {
                MessageBox_LPV.Show("Chọn nguyên liệu cần xóa");
                return;
            }
            if (tb_congthucdinhluong.Select("FLAG=TRUE").Length == 0)
            {
                MessageBox_LPV.Show("Chọn nguyên liệu cần xóa");
                return;
            }
            for (int x = 0; x < tb_temp.Rows.Count; x++)
            {
                if (tb_temp.Rows[x]["ID"] + "" == "")
                    continue;                
                if(str_arr_macongthuc.Trim()=="")
                    str_arr_macongthuc = "" + tb_temp.Rows[x]["ID"];
                else
                    str_arr_macongthuc += "," + tb_temp.Rows[x]["ID"];
            }
            if (str_arr_macongthuc.Trim() != "" && str_arr_macongthuc.IndexOf(",") == 0)
                str_arr_macongthuc = ("," + str_arr_macongthuc.Trim()).Replace(",,", "");

            for (int i = tb_congthucdinhluong.Rows.Count - 1; i >= 0; i--)
            {
                if (tb_congthucdinhluong.Rows[i]["FLAG"] + "" != "")
                {
                    if (Boolean.Parse(tb_congthucdinhluong.Rows[i]["FLAG"] + "") == true)
                        tb_congthucdinhluong.Rows.RemoveAt(i);
                }
                xoa = true;
            }
        }
        private void grid_congthucdinhluong_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grv_congthucdinhluong.RowCount > 0)
            {
            }

        }

        private void bton_dinhluong_luu_Click(object sender, EventArgs e)
        {
            int ketqua = -1;
            Mod_messagebox mess = new Mod_messagebox();
            //if(tb_congthucdinhluong.Columns.Contains("ID"))
            //   tb_congthucdinhluong.Columns.Remove("ID");
            tv = new thuvien();
            ketqua = tv.SP_inser_alltable("DM_CONGTHUCDINHLUONG", tb_congthucdinhluong);
            if (ketqua > -1 && xoa == true && str_arr_macongthuc != "")
            {
                ketqua = f_update_dinhluong(4, 3, 0, "", "", str_arr_macongthuc);
                str_arr_macongthuc = "";
            }
            if (ketqua > -1)
            {
                mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.canhbao.GetHashCode());
                tb_congthucdinhluong = f_select_dinhluong(1, 2, ma_hanghoa);
                tb_congthucdinhluong.Columns.Add(new DataColumn("FLAG", typeof(Boolean)));
                grd_congthucdinhluong.DataSource = tb_congthucdinhluong;
                col_delete.FieldName = "FLAG";
                
                xoa = false;
            }

            else
                mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.canhbao.GetHashCode());

        }
        private void bton_listneed_Click(object sender, EventArgs e)
        {
            if (grid_hanghoa.RowCount > 0)
            {
                mh_dmlistneed frm_listneed = new mh_dmlistneed();
                frm_listneed.str_tenhanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tenhanghoa) + "";
                frm_listneed.str_mahanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa) + "";
                frm_listneed.str_dvt = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_DVT) + "";
                // col_DVT
                frm_listneed.ShowDialog();
            }

        }
        #endregion

        private void btn_Xoa_BOM_LienQuan_Click(object sender, EventArgs e)
        {
            if (grv_nguyenlieu.FocusedRowHandle >= 0)
            {
                clsthongbao = new thongbao_lang();
                DataRow dr = grv_nguyenlieu.GetDataRow(grv_nguyenlieu.FocusedRowHandle);

                int irt = new thuvien().SP_Update("sp_dinhluong", new string[] { "@flag", "@manguyenlieu" }, new object[] { 5, dr["MANGUYENLIEU"] });
                if (irt > 0)
                {
                    // MessageBox_LPV.Show("Xóa ĐỊNH LƯỢNG thành công");
                    clsthongbao.show_info(str_lang, "thanhcong");
                    tb_nguyenlieu = f_select_dinhluong(1, 1, "");
                    grd_nguyenlieu.DataSource = tb_nguyenlieu;
                }
            }
        }

        private void grid_hanghoa_Click(object sender, EventArgs e)
        {
            if (grid_hanghoa.RowCount <= 0)
                this.grid_hanghoa_FocusedRowChanged(null, null);
        }

        private void panel_hinh_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF";

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                txt_pathhinh.Text = dlg.FileName;
                //pichinh_anh.ImageLocation = dlg.FileName;
                Image img = Image.FromFile(dlg.FileName);
                panel_hinh.BackgroundImage = img;
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            this.Cursor=Cursors.WaitCursor;
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT OBJID, HINH FROM DM_HH";
            DataTable tbHH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL"}, new object[] { sSQL});
            cpublic cp = new cpublic();
            foreach (DataRow dr in tbHH.Rows)
            {
                try
                {
                    string sPath = Application.StartupPath + "/hhh/" + dr["OBJID"] + ".jpg";
                    System.IO.FileInfo fi = new System.IO.FileInfo(sPath);
                    if (fi.Exists)
                    {
                        fi.Delete();
                    }
                    else if (!fi.Directory.Exists)
                        fi.Directory.Create();
                    if (dr["HINH"] + "" != "")
                    {
                        Image img = cp.ConvertByteArrayToImage(dr["HINH"]);
                        img.Save(sPath);
                    }
                }
                catch { }
            }
            this.Cursor = Cursors.Default;
            MessageBox.Show("Hoàn tất");
        }

        private void chk_Display_PicItem_CheckedChanged(object sender, EventArgs e)
        {
            new cpublic().Registry_Set("chk_Display_PicItem", chk_Display_PicItem.Checked);
            btnDownload.Enabled = chk_Display_PicItem.Checked;
        }

        private void num_Item_SizeX_EditValueChanged(object sender, EventArgs e)
        {

            new cpublic().Registry_Set("num_Item_SizeX", num_Item_SizeX.Value);
            

        }

        private void num_Item_SizeY_EditValueChanged(object sender, EventArgs e)
        {
            new cpublic().Registry_Set("num_Item_SizeY", num_Item_SizeY.Value);
        }

       
    }
}

